/*
 * @Description: 视频页面
 * @Author: pinus
 * @Email: pinus0716@163.com
 * @Git: https://gitee.com/xyf97
 * @Date: 2021-10-24 17:18:08
 * @LastEditTime: 2021-10-29 12:01:31
 */
// pages/video.js
import request from '../../utils/request'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    toLogin: true,
    categoryList: [],
    cateId: 0, // 当前选中的时频类id
    videoList: [],
    vid: 0, // 当前操作的视频名称,
    videosTimeStamp: {}, // 保存播放事件
    isRefresher: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // 1 获取登录cookies
    if (wx.getStorageSync('cookies')) {
      this.setData({
        toLogin: false
      })

      // 2 获取视频分类列表
      let categoryList = await request('/video/category/list')
      // console.log(categoryList)
      this.setData({
        categoryList,
        cateId: categoryList.data[0].id
      })

      // 获取对应视频
      let videoList = await request('/video/group', {
        id: this.data.cateId
      })
      let id = 0
      videoList = videoList.datas.map(item => {
        item.id = id++
        return item
      })
      // 获取url
      for (let i in videoList) {
        let info = await request('/video/url', {
          id: videoList[i].data.vid
        })
        videoList[i].url = info.urls[0].url
      }

      this.setData({
        videoList: videoList
      })
    }
  },

  // 未登录
  switchToIndex() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  switchToLogin() {
    wx.reLaunch({
      url: '/pages/login/login'
    })
  },
  // 切换类
  async cateChange(event) {
    if (this.cateId !== event.currentTarget.id) {
      this.setData({
        cateId: event.currentTarget.id,
        videoList: []
      })
      wx.showLoading({
        title: '正在加载',
        mask: true
      })

      // 获取对应视频
      let videoList = await request('/video/group', {
        id: event.currentTarget.id
      })
      let id = 0
      videoList = videoList.datas.map(item => {
        item.id = id++
        return item
      })
      // 获取url
      for (let i in videoList) {
        let info = await request('/video/url', {
          id: videoList[i].data.vid
        })
        videoList[i].url = info.urls[0].url
      }
      this.setData({
        videoList: videoList
      })
      wx.hideLoading()
    }
  },
  // 控制视频
  videoCtrl(event) {
    // console.log(event)
    //
    if (this.data.vid !== event.currentTarget.id) {
      this.setData({
        vid: event.currentTarget.id
      })
    }

    const VideoContext = wx.createVideoContext(this.data.vid)
    if (this.data.videosTimeStamp.hasOwnProperty(this.data.vid)) {
      VideoContext.seek(this.data.videosTimeStamp[this.data.vid])
    }
    VideoContext.play()
  },
  // 视频进度变化
  handleTimeUpdate(event) {
    // console.log(event)
    let videosTimeStamp = this.data.videosTimeStamp
    videosTimeStamp[event.currentTarget.id] = event.detail.currentTime
    this.setData({
      videosTimeStamp
    })
  },
  // 视频播放结束
  handleEnd(event) {
    // console.log(event)
    let videosTimeStamp = this.data.videosTimeStamp
    delete videosTimeStamp[event.currentTarget.id]
    this.setData({
      videosTimeStamp
    })
  },
  // 下拉刷新
  async hanldeRefresherPull() {
    this.setData({
      isRefresher: true
    })

    // 获取对应视频
    let videoList = await request('/video/group', {
      id: this.data.cateId
    })
    let id = 0
    videoList = videoList.datas.map(item => {
      item.id = id++
      return item
    })
    // 获取url
    for (let i in videoList) {
      let info = await request('/video/url', {
        id: videoList[i].data.vid
      })
      videoList[i].url = info.urls[0].url
    }
    this.setData({
      videoList: videoList,
      isRefresher: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})
